package com.dbh.alg.datastructure;

/**
 * 链表的中间节点
 * @author 董帮辉
 * @date 2025/5/10
 **/
public class E08Leetcode876 {

    /**
     * 链表的中间节点
     * @param head 链表头
     * @return 链表的中间节点
     */
    public static ListNode middleNode(ListNode head) {
        ListNode p1 = head;
        ListNode p2 = head;
        while (p2 != null && p2.next != null) {
            p1 = p1.next;
            p2 = p2.next;
            p2 = p2.next;
        }
        return p1;
    }

    public static void main(String[] args) {
        ListNode o1 = ListNode.of(1, 2, 3, 4, 5);
        ListNode o2 = ListNode.of(1, 2, 3, 4, 5, 6);
        System.out.println(middleNode(o1));
        System.out.println(middleNode(o2));
    }
}
